#include "fileworker.h"
#include <QDebug>
#include <Windows.h>
#include <QFileDialog>

FileWorker::FileWorker(QObject *parent) : QObject(parent)
{

}

FileWorker::~FileWorker()
{
    //注意指针释放的顺序、同时注意不能释放空指针
    freeExcel();
    delete pSheet;
    delete pSheets;
    delete pWorkBook;
    delete pWorkBooks;
    delete pApplication;
}

void FileWorker::initExcel()
{
    // 在文件线程创建时调用
    CoInitializeEx(NULL, COINIT_MULTITHREADED);
    pApplication = new QAxObject();
    if (!pApplication->setControl("Excel.Application"))
    {
        pApplication->setControl("ket.Application");
        qDebug() << "打开Excel失败，使用WPS替代";
    }
    createExcel();
}

void FileWorker::createExcel()
{
    QString fileName = QFileDialog::getSaveFileName(NULL, "请选择要保存数据的位置", ".", "Excel File(*.xls)");
    fileName.replace("/", "\\");    //修改格式问题
    excleFileName = fileName;
    qDebug() << "获取到文件路径" << fileName;
    if (pApplication == nullptr) {
        qWarning("pApplication错误！\n");
        return;
    }
    pApplication->dynamicCall("SetVisible(bool)",false);// false不显示窗体
    pApplication->setProperty("DisplayAlerts",false);// 不显示任何警告信息
    pWorkBooks = pApplication->querySubObject("Workbooks");
    QFile file(fileName);
    if (file.exists()) {
        pWorkBook = pWorkBooks->querySubObject("Open(const QString&)",fileName, 3, false);
    } else {
        pWorkBooks->dynamicCall("Add");
        pWorkBook = pApplication->querySubObject("ActiveWorkBook");
        pWorkBook -> dynamicCall("SaveAs(const QString&)", fileName);
    }
    pSheets = pWorkBook->querySubObject("Sheets");
    pSheet = pSheets->querySubObject("Item(int)",1);

    QAxObject *cell_1_1 = pSheet->querySubObject("Cells(int,int)", 1, 1);
    cell_1_1->setProperty("Value2", "压力数据");
    QAxObject *cell_2_1 = pSheet->querySubObject("Cells(int,int)", 1, 2);
    cell_2_1->setProperty("Value2", "流量数据");
    QAxObject *cell_3_1 = pSheet->querySubObject("Cells(int,int)", 1, 3);
    cell_3_1->setProperty("Value2", "温度数据");
    file.close();   //关闭文件
}

void FileWorker::freeExcel()
{
    //释放资源前保存
    pWorkBook->dynamicCall("SaveAs (const QString&,int,const QString&,const QString&,bool,bool)",
                                 excleFileName,56,QString(""),QString(""),false,false);
    pWorkBook->dynamicCall("Close(Boolean)", false);
    pApplication->dynamicCall("Quit(void)");
}

void FileWorker::saveExcelData(QString flowData, QString pressData, QString tempData)
{
    QAxObject *cell_3_1 = pSheet->querySubObject("Cells(int,int)", autoExcelRows, 1);
    cell_3_1->setProperty("Value2", flowData);
    QAxObject *cell_2_2 = pSheet->querySubObject("Cells(int,int)", autoExcelRows, 2);
    cell_2_2->setProperty("Value2", pressData);
    QAxObject *cell_2_3 = pSheet->querySubObject("Cells(int,int)", autoExcelRows, 3);
    cell_2_3->setProperty("Value2", tempData);
    autoExcelRows++;
}

int FileWorker::getExcelDataRows()
{
    useRange = pSheet->querySubObject("Usedrange"); //获取权限
    int rows = useRange->property("Row").toInt();  //获取数据行数
    qDebug() << "获取到的数据条数："  << rows;
    delete useRange;
    return rows;
}
